package com.banciyuan.bcywebview.biz.detail.charge;

import android.content.Context;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.PayApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import com.banciyuan.bcywebview.utils.http.BcyPair;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.ChargeItem;

/**
 * Created by squall on 2016/5/16.
 */
public class PayHelper {

    private Context mContext;
    private RequestQueue queue;
    private PayHelperInter IpayHelper;

    public PayHelper(Context mContext) {
        this.mContext = mContext;
        this.queue = VolleyQueue.getRquest(mContext);
    }

    public void getPayOrder(final PayHelperInter ipayHelper, ChargeItem chargeItem) {

        String url = HttpUtils.BASE_URL + PayApi.getPayOrder();

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    ipayHelper.Succ(response);
                } else {
                    ipayHelper.fail();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ipayHelper.fail();
            }
        };

        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        pairs.add(new BcyPair("charge_type", chargeItem.getCharge_type()));
        pairs.add(new BcyPair(HttpUtils.POSTID, chargeItem.getPost_id()));
        pairs.add(new BcyPair("post_uid", chargeItem.getPost_uid()));
        pairs.add(new BcyPair("post_type", chargeItem.getPost_type()));
        Map<String, String> map = HttpUtils.getData(pairs);
        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError) {
            @Override
            public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
                return super.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1f));
            }
        };
        queue.add(stringRequestParam);
    }

    public void getPayList(final PayHelperInter ipayHelper, ChargeItem chargeItem, int page) {
        if (chargeItem == null) return;
        String url = HttpUtils.BASE_URL + PayApi.getPayTopList();
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    ipayHelper.Succ(response);
                } else {
                    ipayHelper.fail();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ipayHelper.fail();
            }
        };

        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair("post_type", chargeItem.getPost_type()));
        pairs.add(new BcyPair(HttpUtils.POSTID, chargeItem.getPost_id()));
        pairs.add(new BcyPair(HttpUtils.INDEX_P, String.valueOf(page)));


        Map<String, String> map = HttpUtils.getData(pairs);
        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        queue.add(stringRequestParam);
    }

    public void getPayStatus(final PayHelperInter ipayHelper, String order_no) {
        String url = HttpUtils.BASE_URL + PayApi.getPayOrderStatus();
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    ipayHelper.Succ(response);
                } else {
                    ipayHelper.fail();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ipayHelper.fail();
            }
        };

        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        pairs.add(new BcyPair("sn", order_no));
        Map<String, String> map = HttpUtils.getData(pairs);
        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        queue.add(stringRequestParam);
    }
}
